/**
 * 
 */
package com.nail.zhaogr.front.action;

import java.util.Enumeration;
import java.util.List;

import javacommon.base.BaseStruts2Action;

import org.apache.struts2.ServletActionContext;

import cn.org.rapid_framework.page.Page;

import com.nail.zhaogr.model.City;
import com.nail.zhaogr.model.Comments;
import com.nail.zhaogr.model.Members;
import com.nail.zhaogr.model.PublishInfo;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

/**
 * @author Administrator
 * 
 */
public abstract class BaseAction extends BaseStruts2Action implements
		Preparable, ModelDriven {

	private String metaDesc;

	private String title;
	private String url;
	private String index;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getMetaDesc() {
		return metaDesc;
	}

	public void setMetaDesc(String metaDesc) {
		this.metaDesc = metaDesc;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	protected void getPageUrl() {
		String requestUrl = ServletActionContext.getRequest().getRequestURL().toString();
		Enumeration list = ServletActionContext.getRequest().getParameterNames();
		int i=0;
		while(list.hasMoreElements()){
			String key = list.nextElement()+"";
			if(!key.equals("pageNumber")&&!key.equals("pageSize")){
				if(i==0){
					requestUrl +="?"+key+"="+ServletActionContext.getRequest().getParameter(key);
				}else{
					requestUrl +="&"+key+"="+ServletActionContext.getRequest().getParameter(key);
				}
				i++;
			}
		}
		if(i==0){
			requestUrl +="?temp=1";
		}
		this.url = requestUrl;
	}

	public Members getMemberInfo() {
		Object obj = this.getRequest().getSession().getAttribute("memberInfo");
		if (obj != null) {
			return (Members) obj;
		} else {
			return null;
		}
	}

	protected boolean filterPublishInfoKeywords(PublishInfo publishInfo) {
		String dirtywords = System.getProperty("dirtywords");
		String[] dirtywordsArray = dirtywords.split(",");
		for (int i = 0; i < dirtywordsArray.length; i++) {
			if (publishInfo.getName().indexOf(dirtywordsArray[i]) != -1
					|| publishInfo.getAddress().indexOf(dirtywordsArray[i]) != -1
					|| publishInfo.getProfile().indexOf(dirtywordsArray[i]) != -1
					|| publishInfo.getPhone().indexOf(dirtywordsArray[i]) != -1) {
				return false;
			}
		}
		return true;
	}

	protected boolean filterCommentsKeywords(Comments comments) {
		String dirtywords = System.getProperty("dirtywords");
		String[] dirtywordsArray = dirtywords.split(",");
		for (int i = 0; i < dirtywordsArray.length; i++) {
			if (comments.getContents().indexOf(dirtywordsArray[i]) != -1) {
				return false;
			}
		}
		return true;
	}
	
	protected City getCurrentCity(){
		Object obj = this.getRequest().getSession().getAttribute("currentCity");
		if (obj != null) {
			return (City) obj;
		} else {
			City city = new City();
			city.setCityId(1372L);
			city.setName("成都市");
			//city.setParentCityId(value);
			return city;
		}
	}

	public String getIndex() {
		return index;
	}

	public void setIndex(String index) {
		this.index = index;
	}
	

	protected void limitLength(Page page){
		List list = page.getResult();
		for (int i = 0; i < list.size(); i++) {
			PublishInfo publishInfo = (PublishInfo)list.get(i);
			String profile = publishInfo.getProfile();
			if(profile.length()>50){
				profile = profile.substring(0,50) +"...";
			}
			publishInfo.setProfile(profile);
		}
	}

}
